# Maintainer: Fabio 'Lolix' Loli <fabio.loli@disroot.org> -> https://github.com/FabioLolix
# Contributor: Matthias Mailänder <matthias@mailaender.name>
# Contributor: dreieck
# Contributor: Eric Bélanger <eric@archlinux.org>

pkgname=python2-wxpython3
pkgver=3.0.2.0
pkgrel=14
pkgdesc="Classic wxWidgets GUI toolkit for Python"
arch=(x86_64 aarch64)
license=('custom:wxWindows')
url="https://www.wxpython.org"
depends=(wxwidgets-gtk3-3.0 python2)
makedepends=(mesa glu)
conflicts=(wxpython)
replaces=(wxpython)
provides=(wxpython=$pkgver)
source=("https://downloads.sourceforge.net/wxpython/wxPython-src-${pkgver}.tar.bz2"
        "fix-plot.patch"
        "fix-editra-removal.patch")
sha256sums=('d54129e5fbea4fb8091c87b2980760b72c22a386cb3b9dd2eebc928ef5e8df61'
            '2d8b2bdbf55172738c7bf93955e552aac61842800c3b8b0a2e1f07f4314abc65'
            '559b076f206c44e66b33857f49068d66f59a3fc15b97972486e32a8105885554')

prepare() {
    find . -type f -exec sed -i 's/env python/env python2/' {} \;

    cd wxPython-src-${pkgver}/wxPython
    # Fix plot library (FS#42807)
    patch -Np1 -i ../../fix-plot.patch

    # Fix editra removal (FS#63563)
    patch -Np2 -i ../../fix-editra-removal.patch
}

build() {
# Old, non-maintained software. Use older C/C++ standard.
_C_std='gnu++14'
_CXX_std='gnu++14'

# In case a newer C standard is used, warnings are generated which should not be treated as errors.
_append_to_CFLAGS_after_configure=' -Wno-error -Wno-error=format-security -Wno-error=register -Wno-error=deprecated-declarations -Wno-error=alloc-size-larger-than= -Wno-error=write-strings -Wno-error=return-local-addr -Wno-error=attributes' # Or just use -Wno-error to catch all.
_append_to_CXXFLAGS_after_configure="${_append_to_CFLAGS_after_configure}"

  CFLAGS+=" -std=${_C_std}"
  CXXFLAGS+=" -std=${_CXX_std}"
  export CFLAGS
  export CXXFLAGS

  cd wxPython-src-${pkgver}
  if [[ $CARCH == "aarch64" ]]; then
    ./configure "${_configure_opts[@]}" \
        --prefix=/usr \
        --host="$CARCH" \
        --libdir=/usr/lib \
        --includedir=/usr/include \
        --with-gtk=3 \
        --with-opengl \
        --enable-unicode \
        --enable-graphics_ctx \
        --disable-precomp-headers \
        --with-regex=sys \
        --with-libpng=sys \
        --with-libxpm=sys \
        --with-libjpeg=sys \
        --with-libtiff=sys \
        --with-wx-config=/opt/wxgtk-3.0/bin/wx-config-gtk3
  else
    ./configure "${_configure_opts[@]}" \
        --prefix=/usr \
        --libdir=/usr/lib \
        --includedir=/usr/include \
        --with-gtk=3 \
        --with-opengl \
        --enable-unicode \
        --enable-graphics_ctx \
        --disable-precomp-headers \
        --with-regex=sys \
        --with-libpng=sys \
        --with-libxpm=sys \
        --with-libjpeg=sys \
        --with-libtiff=sys \
        --with-wx-config=/opt/wxgtk-3.0/bin/wx-config-gtk3
  fi

  echo "CONFIG DONE"

  CFLAGS+="${_append_to_CFLAGS_after_configure} -std=${_C_std}"
  CXXFLAGS+="${_append_to_CXXFLAGS_after_configure} -std=${_CXX_std}"
  export CFLAGS
  export CXXFLAGS

  make

  cd wxPython
  python2 setup.py WX_CONFIG=/opt/wxgtk-3.0/bin/wx-config-gtk3 WXPORT=gtk3 UNICODE=1 build
}

package() {
    cd "wxPython-src-${pkgver}/wxPython"
    python2 setup.py WX_CONFIG=/opt/wxgtk-3.0/bin/wx-config-gtk3 WXPORT=gtk3 UNICODE=1 install --root="${pkgdir}" --optimize=1

    install -d "${pkgdir}"/usr/include/wx-3.0/wx/wxPython
    cp -r "${pkgdir}"/opt/wxgtk-3.0/include/wx-3.0/wx/wxPython/* "${pkgdir}"/usr/include/wx-3.0/wx/wxPython
    rm -r "${pkgdir}"/opt

    for file in "${pkgdir}"/usr/bin/*; do mv "${file}" "${file}2"; done
    install -Dm644 ../docs/licence.txt "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}
